<template>
    <div class="login">
      <!-- 头部 NavBar -->
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)">
    </van-nav-bar>

      <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="picNumber">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="picCode">
          <img v-if="picUrl" :src="picUrl" alt="" @click="getPicCode()">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码  (默认：246810)" type="text" v-model="smsCode">
          <button @click="getCode()">{{(second >=60 ) ? "获取验证码" :second+"秒后重新发送"}}</button>
        </div>
      </div>

      <div class="login-btn" @click="login()">登录</div>
    </div>
    </div>
</template>

<script>
import { getLogin, getMsgCode, getPicCode } from '@/api/login'
// import { Toast } from 'vant'
// import request from '@/utils/request'

export default {
  name: 'LoginPage',
  data () {
    return {
      picNumber: '', // 手机号码
      picCode: '', // 用户输入的图形化验证码
      picKey: '', // 将来请求传递的图形验证码唯一表示
      picUrl: '', // 存储请求渲染的图片地址
      // totalSecond:60, // 总秒数
      second: 60, // 当前秒数
      timer: null, // 定时器id
      smsCode: ''// 手机短信验证码

    }
  },
  async created () {
    this.getPicCode()
  },
  methods: {
    async getPicCode () {
    // 解构
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64// 存储地址
      this.picKey = key// 存储唯一标识
    },
    // 校验手机号 和 图形验证码 是否合法
    // 通过校验返回ture,否则返回false
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.picNumber)) {
        this.$toast('请输入正确的手机号码')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },

    // 获取验证码
    async getCode () {
      if (this.second === 60) {
        // 发送请求
        await getMsgCode(this.picCode, this.picKey, this.picNumber)
        // console.log(res)
        this.$toast('短信发送成功')

        // 倒计时
        if (!this.validFn()) return
        // 节流
        if (!this.timer) {
          this.timer = setInterval(() => {
            this.second--
            if (this.second <= 0) {
              clearInterval(this.timer)// 关闭定时器
              this.second = 60// 重置秒数
            }
          }, 1000)
        }
      }
    },
    // 登录
    async login () {
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的验证码')
      }
      const res = await getLogin(this.picNumber, this.smsCode)
      // 存储个人权证相关
      this.$store.commit('user/setUserInfo', res.data)
      // console.log(res.data)
      this.$toast('登录成功')
      // 判断是否有回跳参数,有则需要拦截回调 , 没有则跳到登录页
      const url = this.$route.query.backUrl || '/'
      this.$router.replace(url)
    }
  },
  destroyed () {
    clearInterval(this.timer)// 离开页面清除定时器
  }

}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
